package com.borya.filter.auth.impl;

import java.util.Objects;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.borya.filter.auth.AppSignTokenVersion;
import com.borya.filter.auth.AppType;
import com.borya.filter.auth.AuthHelper;
import com.borya.filter.auth.AuthOperation;
import com.borya.filter.auth.AuthRequest;
import com.borya.filter.auth.AuthResponse;
import com.borya.filter.auth.AuthService;
import com.borya.statuscode.StatusCode;
import com.borya.util.SystemConfig;

public class UmsOperation extends AuthService implements AuthOperation {

	private Logger log = Logger.getLogger(getClass());

	@Override
	public AuthResponse apply(AuthRequest request) {
		SystemConfig instance = SystemConfig.getInstance();
		AuthResponse authResponse = new AuthResponse();
		if (AuthHelper.WEB == request.getOsType()) { // 页面请求
			log.warn("请求url不规范:" + request.getUrl());
			return setResponse(true, JSONObject.parseObject(request.getBody()), authResponse);
		}
		if (Objects.equals(AppType.KA_MENG.getType(), request.getAppType())) { // 卡盟
			// 校验Sign
			return decryptAuthBody(request, authResponse);
		} else if (Objects.equals(AppType.XI_NIU.getType(), request.getAppType())) { // 喜牛
			// 校验Sign
			authResponse = checkSign(request, authResponse,
					instance.getInteger(AppSignTokenVersion.XI_NIU_ANDROID.getSign()),
					instance.getInteger(AppSignTokenVersion.XI_NIU_IOS.getSign()));
			if (!authResponse.getAccess()) {
				return authResponse;
			}
			return decryptAuthBody(request, authResponse);
		} else if (Objects.equals(AppType.YI_JIA.getType(), request.getAppType())) { // 亿嘉
			// 校验Sign
			authResponse = checkSign(request, authResponse,
					instance.getInteger(AppSignTokenVersion.YI_JIA_ANDROID.getSign()),
					instance.getInteger(AppSignTokenVersion.YI_JIA_IOS.getSign()));
			if (!authResponse.getAccess()) {
				return authResponse;
			}
			return decryptAuthBody(request, authResponse);
		} else if (Objects.equals(AppType.QUAN_WANG_TONG.getType(), request.getAppType())) { // 全通
			// 校验Sign
			authResponse = checkSign(request, authResponse,
					instance.getInteger(AppSignTokenVersion.QUAN_WANG_TONG_ANDROID.getSign()),
					instance.getInteger(AppSignTokenVersion.QUAN_WANG_TONG_IOS.getSign()));
			if (!authResponse.getAccess()) {
				return authResponse;
			}
			return decryptAuthBody(request, authResponse);
		} else if (Objects.equals(AppType.JU_LIAN.getType(), request.getAppType())) { // 聚联
			// 校验Sign
			authResponse = checkSign(request, authResponse,
					instance.getInteger(AppSignTokenVersion.JU_LIAN_ANDROID.getSign()),
					instance.getInteger(AppSignTokenVersion.JU_LIAN_IOS.getSign()));
			if (!authResponse.getAccess()) {
				return authResponse;
			}
			return decryptAuthBody(request, authResponse);
		} else if (Objects.equals(AppType.BEIDOU_GUOXING.getType(), request.getAppType())) { // 北斗国星
			return setResponse(true, JSONObject.parseObject(request.getBody()), authResponse);
		} else if (Objects.equals(AppType.KM_SDK.getType(), request.getAppType())) {
			return decryptAuthBody(request, authResponse);
		} else {
			log.warn("wrong appType, authRequest: " + request);
			setResponse(false, StatusCode.FORBID.toJSON(), authResponse);
		}
		return authResponse;
	}

	/**
	 * 校验sign
	 *
	 * @param request
	 *            请求
	 * @param authResponse
	 *            响应
	 * @return
	 */
	private AuthResponse decryptAuthBody(AuthRequest request, AuthResponse authResponse) {
		return setResponse(true, decryptBody(request), authResponse);
	}

	/**
	 * 校验sign，低版本不能通过
	 *
	 * @param request
	 *            请求
	 * @param authResponse
	 *            响应
	 * @param androidTargetVersion
	 *            安卓比对版本
	 * @param iosTargetVersion
	 *            苹果比对版本
	 * @return
	 */
	private AuthResponse checkSign(AuthRequest request, AuthResponse authResponse, int androidTargetVersion,
			int iosTargetVersion) {
		if (Objects.isNull(request.getSign())) {
			log.warn(String.format("host: %s, sign不能为空", request.getHost()));
			return setResponse(false, StatusCode.FORBID.toJSON(), authResponse);
		}
		authResponse.setAccess(true);
		return authResponse;
	}

	/**
	 * 设置authResponse
	 *
	 * @param access
	 *            能否访问
	 * @param msg
	 *            返回消息
	 * @param authResponse
	 *            响应
	 * @return
	 */
	private AuthResponse setResponse(boolean access, String msg, AuthResponse authResponse) {
		authResponse.setAccess(access);
		authResponse.setMsg(msg);
		return authResponse;
	}

	private AuthResponse setResponse(boolean access, JSONObject json, AuthResponse authResponse) {
		authResponse.setAccess(access);
		authResponse.setJson(json);
		return authResponse;
	}

}
